

<template>
  <view class="container">
	  
	  <!-- 新增收货地址页面 -->
	  
	<view style="padding-bottom: 10px;display: flex;justify-content:flex-end;">
		<button @click="handleAddAddress" type="primary" size="mini" plain="true" style="color: #515151;width: 100px;margin: 0;border-radius: 25px;border: none;background: rgba(255, 255, 255, 0.6);backdrop-filter: blur(3px);">新增地址</button>
	</view>
	<scroll-view style="padding-bottom: 10px;" scroll-top="0" scroll-y="true"  :style="{height: `${windowHeight-100}px`}">
		
		<view v-if="this.addressList.length > 0">
			<view v-for="(item, index)  in addressList" :key="index" style="background-color: white;border-radius: 10px;margin-bottom: 10px;padding: 10px;display: flex;flex-direction: column;">
				<view style="padding: 5px 0 5px 0;display: flex;flex-direction: row;align-items: flex-end;">
					<text style="color: black;font-weight: 600;font-size: 15px;">
						{{item.addressName}}
					</text>
					<text <text style="color: black;font-weight: 600;font-size: 16px;padding-left: 10px;">
						{{item.addressMobile}}
					</text>
					<uni-tag v-if="item.isDefAddress == 1" style="margin-left: 10px;" :inverted="true" text="默认地址" type="error" />
				</view>
				<view style="padding: 5px 0 5px 0;">
					<text class="uni-body">
						{{item.address}}
					</text>
				</view>
				<view style="padding: 5px 0 5px 0;display: flex;flex-direction: row;align-items: flex-end;justify-content: space-between;">
					<!-- 是否默认地址0:否，1:是 -->
					<view v-if="item.isDefAddress == 0"  @click="actionsSetDefClick(item)" style="display: flex;align-items: center;border: 1px solid grey;border-radius: 25px;padding: 3px;width: 100px;justify-content: center;">
						<text style="font-size: 14px;color: #666;margin-left: 5px;">使用该地址</text>
					</view>
					<!-- 占位 -->
					<view v-if="item.isDefAddress == 1" style="width: 100px;">
					</view>					
					<view @click="actionsDelClick(item)" style="display: flex;align-items: center;border: 1px solid grey;border-radius: 25px;padding: 3px;width: 45px;justify-content: center;">
						<!-- <uni-icons type="trash" size="18" color="#999"></uni-icons> -->
						<text style="font-size: 14px;color: #666;margin-left: 5px;">删除</text>
					</view>				
				</view>
			</view>

		</view>
		<view v-if="this.addressList.length == 0">
			<view style="text-align: center;">
				<text class="uni-body">暂无数据</text>
			</view>
		</view>
	</scroll-view>
	
  </view>
</template>

<script>
	
  export default {
	name:'addressList',
	onLoad(option) {//option为object类型，会序列化上个页面传递的参数
		//console.log(option.id); //打印出上个页面传递的参数。
		//console.log(option.name); //打印出上个页面传递的参数。
	},
	onShow() {
		console.log('____onShow__')
		this.initData();
	},
	computed:{
	  windowHeight() {
		return uni.getSystemInfoSync().windowHeight - 0
	  }
	},
    data() {
      return {
		  userInfo:this.$store.state.user.userinfo,
		  addressList:[],
		  txCloudEnvID:getApp().globalData.config.appInfo.txCloudEnvID,
		  vxService:getApp().globalData.config.appInfo.vxService,
      }
    },
	created() {
		//console.log('____created__')
	},
    methods: {
	  initData(){
		if(this.userInfo){
			this.addressList = [];
			console.log(this.userInfo.clientId,'___收货地址列表____userInfo_客户主键di__')
			wx.cloud.callContainer({
			  "config": {
			    "env": this.txCloudEnvID
			  },
			  "path": "/system/address/listByClientId?clientId="+this.userInfo.clientId,
			  "header": {
			    "X-WX-SERVICE": this.vxService,
			    "content-type": "application/json"
			  },
			  "method": "GET",
			  "data": ""
			}).then((res) =>{
				//console.log(res);
				this.addressList = res.data.data;
				console.log(this.addressList,'__this.addressList__',this.addressList.length);
				
				// this.addressList.forEach((item) =>{
				// 	console.log(item.addressName,'___',item.addressMobile)
				// })
				
			}).catch((e) =>{
				console.log(e.message);
				if(e.message.indexOf('102002') !== -1){
					this.$modal.showToast('请求超时')
				}else{
					this.$modal.showToast(e.message)
				}
			})
		}			
	  },
	  handleAddAddress(){
		  this.$tab.navigateTo('/pages/mine/address/addAddress?clientId='+this.userInfo.clientId)
	  },
	  actionsDelClick(data){
		this.$modal.confirm('是否删除：'+data.addressName+' 数据项').then(() => {
		    console.log(data,'——删除地址——__主键id:_',data.addressId)
			wx.cloud.callContainer({
			  "config": {
			    "env": this.txCloudEnvID
			  },
			  "path": "/system/address/"+data.addressId,
			  "header": {
			    "X-WX-SERVICE": this.vxService,
			    "content-type": "application/json"
			  },
			  "method": "DELETE",
			  "data": ""
			}).then((res) =>{
				console.log(res);
				this.$modal.showToast('删除成功！')
				this.initData();
			}).catch((e) =>{
				console.log(e.message);
				if(e.message.indexOf('102002') !== -1){
					this.$modal.showToast('请求超时')
				}else{
					this.$modal.showToast(e.message)
				}
			})
		});
	  },
	  actionsSetDefClick(data) {
	    //this.$tab.navigateTo(`/pages/common/textview/index?title=${item.title}&content=${item.content}`)
		console.log(data,'——设为默认——')
		this.$modal.confirm('是否使用：'+data.addressName+' 该地址').then(() => {
			wx.cloud.callContainer({
			  "config": {
			    "env": this.txCloudEnvID
			  },
			  "path": "/system/address",
			  "header": {
			    "X-WX-SERVICE": this.vxService,
			    "content-type": "application/json"
			  },
			  "method": "PUT",
			  "data": data
			}).then((res) =>{
				console.log(res);
				//this.$modal.showToast('操作成功！')
				//this.initData();
				this.$tab.navigateBack();
			}).catch((e) =>{
				console.log(e.message);
				if(e.message.indexOf('102002') !== -1){
					this.$modal.showToast('请求超时')
				}else{
					this.$modal.showToast(e.message)
				}
			})
		});
	  },
	  
	  
	  
    }
  }
</script>

<style lang="scss" scoped>
  page {
    background-color: #F6F6F6;
  }
  .container {
    margin-bottom: 100rpx;
    padding: 30rpx;
  }


</style>



